﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
	public class AbsenceBLL : IAbsenceBLL
	{
		private static readonly IAbsenceDAL DAL = DataAccess.CreateAbsences();

		private AbsenceInfo abInfo = new AbsenceInfo();
		private EmployeeInfo empInfo = new EmployeeInfo();
		private AbsenceTypeInfo tInfo = new AbsenceTypeInfo();
		private JobSiteInfo jsInfo = new JobSiteInfo();

        public IList<hrm_ps_absence> GetAllAbsence(string bgm_year, string month, string emp_seq)
        {
            return DAL.GetAllAbsence(bgm_year, month, emp_seq);
        }

        public Decimal GetSumAbsence(string record_status, string bgm_year, string month, string emp_seq) {
            return DAL.GetSumAbsence(record_status, bgm_year, month, emp_seq);
        }

		public IList<AbsenceInfo> ListAbsences()
		{
			return DAL.ListAbsences(new AbsenceInfo(0));
		}
		public IList<AbsenceInfo> ListAbsences(string Employee)
		{
			if (Employee.Length > 0)
				empInfo.Sequence = Int32.Parse(Employee);
			abInfo.Employee = empInfo;
			return DAL.ListAbsences(abInfo);
		}
		public IList<AbsenceInfo> ListAbsencesBySequence(int Sequence)
		{
			return DAL.ListAbsences(new AbsenceInfo(Sequence));
		}

        public IList<hrm_ps_absence> GetAbsence(string working_date, string emp_seq)
        {            
            return new AbsenceDAL().GetAbsence(working_date, emp_seq);
        }

		public IList<AbsenceInfo> TreeViewAbsences(string Employee, string JobSite)
		{
			if (Employee.Length > 0)
				empInfo.Sequence = Int32.Parse(Employee);
			if (JobSite.Length > 0)
			{
				jsInfo.Sequence = Int32.Parse(JobSite);
				empInfo.WorkingJobSite = jsInfo;
			}
			abInfo.Employee = empInfo;
			return DAL.TreeViewAbsences(abInfo);
		}
		public DataSet LoadAbsences(Nullable<int> Sequence, Nullable<int> EmpSequence)
		{
			abInfo.Sequence = 0;

			if (Sequence.HasValue)
			{
				abInfo.Sequence = (int)Sequence;
			}
			if (EmpSequence.HasValue)
			{
				empInfo.Sequence = (int)EmpSequence;
				abInfo.Employee = empInfo;
			}

			return DAL.LoadAbsences(abInfo);
		}
		public void Insert(string AbsenceType, string Employee, DateTime StartDate, string StartTime
				, DateTime EndDate, string EndTime, decimal CountDate, string ContactAddress
				, string ApprovedStatus, string ApprovedBy, string ApprovedPosition
				, Nullable<DateTime> ApprovedDate, string Remarks, int CreatedBy)
		{
			if (Employee.Length > 0)
				empInfo.Sequence = Int32.Parse(Employee);
			if (AbsenceType.Length > 0)
				tInfo.Sequence = Int32.Parse(AbsenceType);

			try
			{
				DAL.Insert(new AbsenceInfo(0, tInfo, empInfo, StartDate, StartTime, EndDate, EndTime, CountDate
					, ContactAddress, ApprovedStatus, ApprovedBy, ApprovedPosition, ApprovedDate
					, Remarks, "", CreatedBy, 0));
			}
			catch
			{
				throw new Exception("");
			}
		}

		public void Update(int Sequence, string AbsenceType, string Employee, DateTime StartDate, string StartTime
				, DateTime EndDate, string EndTime, decimal CountDate, string ContactAddress
				, string ApprovedStatus, string ApprovedBy, string ApprovedPosition
				, Nullable<DateTime> ApprovedDate, string Remarks, int UpdatedBy)
		{
			if (Employee.Length > 0)
				empInfo.Sequence = Int32.Parse(Employee);
			if (AbsenceType.Length > 0)
				tInfo.Sequence = Int32.Parse(AbsenceType);

			try
			{
				DAL.Update(new AbsenceInfo(Sequence, tInfo, empInfo, StartDate, StartTime, EndDate, EndTime, CountDate
					, ContactAddress, ApprovedStatus, ApprovedBy, ApprovedPosition, ApprovedDate
					, Remarks, "", 0, UpdatedBy));
			}
			catch
			{
				throw new Exception("");
			}
		}
		public void Delete(int Sequence, int UpdatedBy)
		{
			try
			{
				DAL.Delete(Sequence, UpdatedBy);
			}
			catch
			{
				throw new Exception("");
			}
		}

		public DataSet GetEmployeeNameList(Nullable<int> EmpSequence)
		{
			int iEmpSeq = 0;
			if (EmpSequence.HasValue)
			{
				iEmpSeq = (int)EmpSequence;
			}

			return DAL.GetEmployeeNameList(iEmpSeq);
		}

        //for service
        public IList<hrm_ps_absence> GetAbsenceDays(string emp_seq)
        {
            return DAL.GetAbsenceDays(emp_seq);
        }
	}
}
